//
//  billListViewCell.h
//  ScrollViews
//
//  Created by qqiMac on 2018/4/9.
//  Copyright © 2018年 Mr.Lee. All rights reserved.
//

#import "MainTabbarController.h"
#import "QQNavigationController.h"
#import "HGHomeVC.h"
#import "HGCircleVC.h"
#import "HGGoodsVC.h"
#import "HGMineVC.h"

@interface MainTabbarController ()<UITabBarControllerDelegate>

@end

@implementation MainTabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置item属性
    [self setupItem];
    
    self.delegate = self;
    
    // 添加所有的子控制器
    [self setupChildVcs];
}

/**
 * 添加所有的子控制器
 */
- (void)setupChildVcs
{
    [self setupChildVc:[[HGHomeVC alloc] init] title:@"首页" image:@"sy" selectedImage:@"sy-w"];

    [self setupChildVc:[[HGGoodsVC alloc] init] title:@"商品" image:@"sp" selectedImage:@"sp-w"];

    [self setupChildVc:[[HGCircleVC alloc] init] title:@"酒圈子" image:@"jqz" selectedImage:@"jqz-w"];

    [self setupChildVc:[[HGMineVC alloc] init] title:@"个人" image:@"gr" selectedImage:@"gr-w"];
}

/**
 * 添加一个子控制器
 * @param title 文字
 * @param image 图片
 * @param selectedImage 选中时的图片
 */
- (void)setupChildVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 包装一个导航控制器
    QQNavigationController *nav = [[QQNavigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
    
    // 设置子控制器的tabBarItem
    nav.tabBarItem.title = title;
    UIImage * img = [UIImage imageNamed:image];
    img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem.image = img;
    
    UIImage * selectedImg = [UIImage imageNamed:selectedImage];
    selectedImg =  [selectedImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem.selectedImage = selectedImg;
}

/**
 * 设置item属性
 */
- (void)setupItem
{
    // UIControlStateNormal状态下的文字属性           
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    // 文字颜色
    normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    // 文字大小
    normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    
    // UIControlStateSelected状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    // 文字颜色
    selectedAttrs[NSForegroundColorAttributeName] = kColorMainTheme;
    
    // 统一给所有的UITabBarItem设置文字属性
    // 只有后面带有UI_APPEARANCE_SELECTOR的属性或方法, 才可以通过appearance对象来统一设置
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}
@end
